import { getToken } from '@/utils/storage'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const whiteList = [ '/login' ]

const routes = [
  {
    path: "/",
    name: "LoginIndex",
    redirect:'/layout',
    component: () => import("../views/layout/index.vue"),
  },
  {
    path: "/layoutHead",
    name: "layoutHead",
    component: () => import("../components/layoutHead.vue"),
  },
  {
    path: "/login",
    name: "LoginIndex",
    component: () => import("../views/login/index.vue"),
  },
  {
    path: "/layout",
    name: "LayoutIndex",
    component: () => import("../views/layout/index.vue"),
    children: [
      {
        path: "/dashboard",
        name: "DashBoard",
        component: () => import("../views/dashboard/index.vue"),
      },
      {
        path: "/article",
        name: "ArticleIndex",
        component: () => import("../views/article/index.vue"),
      },
    ],
  },
]


const router = new VueRouter({
  routes
})

// const token = getToken()
// if(token){
//   next()
// }else{
//   next('/login')
// }

router.beforeEach((to, from, next) => {
	/*
  1-获取token
  2、根据token判断
    有token
      表示已经登录了，可以直接访问，需要放行 next
    无token
      判断是不是有的页面没有token也可以访问  比如 /login  【白名单】  是可以直接访问的
      判断没有token又不能直接去访问的【不是白名单的】  跳转到登录页面
  
  */
	const token = getToken()
	if (token) {
		next()
	} else {
		if (whiteList.includes(to.path)) {
			next()
		} else {
			next('/login')
		}
	}
})

export default router
